package com.kele.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kele.mapper.RoleMapper;
import com.kele.pojo.Role;
import com.kele.pojo.User;
import com.kele.mapper.UserMapper;
import com.kele.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserByUsername(String username) {
        return userMapper.getUserByUsername(username);
    }

    @Override
    public List<Role> getUserRoleById(int userId) {
        return userMapper.getUserRoleById(userId);
    }

    @Override
    public PageInfo<User> getAllUser(int pageNum, int pageSize, Map map) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> allUser = userMapper.getAllUser(map);
        PageInfo<User> userPageInfo = new PageInfo<User>(allUser);
        return userPageInfo;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.getUserByUsername(username);
        if (user == null){
            throw new UsernameNotFoundException("用户不存在!");
        }
        user.setRoles(userMapper.getUserRoleById(user.getId()));
        return user;
    }


}
